package com.design.movieticket.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.movieticket.dto.HallListDto;
import com.design.movieticket.dto.MovieArrangeDto;
import com.design.movieticket.entity.MovieArrange;
import com.design.movieticket.mapper.MovieArrangeMapper;
import com.design.movieticket.service.MovieArrangeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

import java.util.Date;
import java.util.List;

@Service
public class MovieArrangeServiceImpl extends ServiceImpl<MovieArrangeMapper, MovieArrange> implements MovieArrangeService {
    @Resource
    private MovieArrangeMapper movieArrangeMapper;

    @Override
    public List<HallListDto> getHallList(String principal) {
        Long cinemaId = movieArrangeMapper.getCinemaIdByPrincipal(principal);
        return movieArrangeMapper.getHallList(cinemaId);
    }

    @Override
    public List<MovieArrange> getTableData(String movieName, Long hallId, String date){
        String dateOne = null;
        String dateTwo = null;

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = format.parse(date);
            dateOne = format.format(date1);
            dateTwo = dateOne +' '+'2'+'3'+':'+'5'+'9'+':'+'5'+'9';
            dateOne = dateOne +' '+'0'+'0'+':'+'0'+'0'+':'+'0'+'0';
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return movieArrangeMapper.getTableData(movieName, hallId, dateOne, dateTwo);
    }

    @Override
    public void addMovieArrange(MovieArrange movieArrange) {
        super.save(movieArrange);
    }

    @Override
    public void removeMovieArrange(Long id) {
        super.removeById(id);
    }

    @Override
    public List<MovieArrangeDto> wxGetMovieArrangeListByCinemaIdAndMovieName(Long cinemaId, String nowTime, String movieName) {
        String dayNowTime = null;
        String dayEndTime = null;

        Date now = new Date();
        System.out.println("现在是" + now);
        SimpleDateFormat mat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String ned = mat.format(now);
            System.out.println("字符现在是" + ned.substring(11,19));
            Date date1 = format.parse(nowTime);
            dayNowTime = format.format(date1);
            dayEndTime = dayNowTime +' '+'2'+'3'+':'+'5'+'9'+':'+'5'+'9';
            if (ned.substring(8,10).equals(nowTime.substring(8,10))) {
                dayNowTime = dayNowTime + ' ' + ned.substring(11,19);
            }else
            {
                dayNowTime= dayNowTime +' '+'0'+'0'+':'+'0'+'0'+':'+'0'+'0';
            }
            System.out.println("dayNowTime=" + dayNowTime + "end" + dayEndTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return movieArrangeMapper.wxGetMovieArrangeListByCinemaIdAndMovieName(cinemaId, dayNowTime, dayEndTime, movieName);
    }
}
